/*
	This file is part of PropExtractor.

	PropExtractor is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	PropExtractor is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with PropExtractor. If not, see <http://www.gnu.org/licenses/>.
*/

#include "PEIncludes.h"

#include "PEPath.h"

namespace pe
{

	PEPath::PEPath(const wchar_t* a_Path)
	{
		m_DirectoriesCount = 0;

		wchar_t* src;

		// count the number of directories

		src = (wchar_t*)a_Path;
		while (1)
		{
			if (*src == L'\\' || *src == L'/' || *src == 0)
			{
				m_DirectoriesCount++;
			}

			if (*src++ == 0) { break; }
		}

		// get directory names

		m_Directories = new wchar_t*[m_DirectoriesCount];

		src = (wchar_t*)a_Path;
		wchar_t* src_start = (wchar_t*)a_Path;
		wchar_t** dst = m_Directories;
		unsigned int dst_curr = 0;
		m_DirectoryLast = 0;
		while (1)
		{
			if (*src == L'\\' || *src == L'/' || *src == 0)
			{
				unsigned int length = src - src_start + 1;
				dst[dst_curr] = new wchar_t[length];
				memset(dst[dst_curr], 0, length * 2);
				wcsncpy(dst[dst_curr], src_start, length - 1);

				if (!wcschr(dst[dst_curr], '.'))
				{
					m_DirectoryLast++;
				}

				src_start = src + 1;
				dst_curr++;
			}

			if (*src++ == 0) { break; }
		}

		int i = 0;
	}
	
	PEPath::~PEPath()
	{
		delete [] m_Directories;
	}

	int PEPath::GetFileName(wchar_t** a_Target)
	{
		wchar_t* dot = wcschr(m_Directories[m_DirectoriesCount - 1], L'.');
		if (!dot) 
		{
			PE_WSTRCLRCPY(*a_Target, m_Directories[m_DirectoriesCount - 1]);
		}
		else
		{
			unsigned int length = dot - m_Directories[m_DirectoriesCount - 1];
			*a_Target = new wchar_t[length + 1];
			memset(*a_Target, 0, (length + 1) * 2);
			wcsncpy(*a_Target, m_Directories[m_DirectoriesCount - 1], length);
		}

		return PE_OK;
	}

	int PEPath::GetFileExtension(wchar_t** a_Target)
	{
		wchar_t* dot = wcschr(m_Directories[m_DirectoriesCount - 1], L'.');
		if (!dot) { return -1; }

		PE_WSTRCLRCPY(*a_Target, dot + 1);

		return PE_OK;
	}

	int PEPath::GetFileFull(wchar_t** a_Target)
	{
		PE_WSTRCLRCPY(*a_Target, m_Directories[m_DirectoriesCount - 1]);

		return PE_OK;
	}

	int PEPath::GetDirDrive(wchar_t** a_Target)
	{
		PE_WSTRCLRCPY(*a_Target, m_Directories[0]);

		return PE_OK;
	}

	int PEPath::GetDirParent(wchar_t** a_Target)
	{
		PE_WSTRCLRCPY(*a_Target, m_Directories[m_DirectoriesCount - 2]);

		return PE_OK;
	}

	int PEPath::GetDirPath(wchar_t** a_Target)
	{
		*a_Target = new wchar_t[_MAX_PATH];
		memset(*a_Target, 0, _MAX_PATH * 2);
		for (unsigned int i = 0; i < m_DirectoriesCount - 1; i++)
		{
			wcscat(*a_Target, m_Directories[i]);
			wcscat(*a_Target, L"\\");
		}

		return PE_OK;
	}

	int PEPath::GetPath(wchar_t** a_Target)
	{
		*a_Target = new wchar_t[_MAX_PATH];
		memset(*a_Target, 0, _MAX_PATH * 2);
		for (unsigned int i = 0; i < m_DirectoriesCount; i++)
		{
			wcscat(*a_Target, m_Directories[i]);
			if (i < m_DirectoriesCount - 1)
			{
				wcscat(*a_Target, L"\\");
			}
		}

		return PE_OK;
	}

	unsigned int PEPath::GetDirectoryCount() const
	{
		return m_DirectoriesCount;
	}

	const wchar_t* PEPath::GetDirectory(unsigned int a_Index) const
	{
		if (a_Index >= m_DirectoriesCount) { return NULL; }

		return m_Directories[a_Index];
	}

}; // namespace pe